export function useClickOutside<E extends HTMLElement>(target: MaybeRefOrGetter<E  | null>, handler: (ev: MouseEvent) => void) {
    useEffectScope(() => {
        watch(<E>target, (_n, _o, onCleanup) => {
            onCleanup(detachEvent)
            const it = toValue(target)
            if (it) {
                attachEvent()
            }
        }, { immediate: true })
    })
    function attachEvent() {
        setTimeout(() => {
            document.addEventListener("click", _handler)
        }, 0)
    }
    function detachEvent() {
        document.removeEventListener("click", _handler)
    }
    function _handler(ev: MouseEvent) {
        const it = <E>toValue(target)
        const triggerer = <E>ev.target
        if (it && triggerer && !it.contains(triggerer)) {
            handler(ev)
        }
    }
}